/*******************************************************************************
 * Copyright (c) 2013 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
package nz.co.senanque.pizzaorder;

import nz.co.senanque.rules.RulesPlugin;
import nz.co.senanque.vaadinsupport.FieldFactory;
import nz.co.senanque.vaadinsupport.MaduraPropertyWrapper;
import nz.co.senanque.vaadinsupport.application.MaduraSessionManager;
import nz.co.senanque.validationengine.FieldMetadata;
import nz.co.senanque.validationengine.ValidationObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Field;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * @author Roger Parkinson
 *
 */
public class OneFieldWindow extends Window {

	private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger(OneFieldWindow.class);
    
    public static void init(RulesPlugin rulesPlugin, ValidationObject validationObject, String fieldName, Window window, MaduraSessionManager maduraSessionManager)
    {
		FieldMetadata fieldMetadata = validationObject.getMetadata().getFieldMetadata(fieldName);
		if (fieldMetadata == null)
		{
			throw new RuntimeException("Failed to find field "+fieldName);
		}
		// this resets all the unknown fields making the scenario repeatable
		rulesPlugin.clearUnknowns(validationObject);
		FieldMetadata fm =  rulesPlugin.getEmptyField(fieldMetadata);
		if (fm != null)
		{
			OneFieldWindow ofw = new OneFieldWindow(rulesPlugin,fm,fieldMetadata,maduraSessionManager);
			window.addWindow(ofw);
		}
		else
		{
			OneFieldWindow ofw = new OneFieldWindow(rulesPlugin,fieldMetadata,maduraSessionManager);
			window.addWindow(ofw);
		}
    }

	private OneFieldWindow(final RulesPlugin rulesPlugin, final FieldMetadata fm,final FieldMetadata fieldMetadata, final MaduraSessionManager maduraSessionManager)
	{
		setModal(true);
        VerticalLayout layout = (VerticalLayout) getContent();
        layout.setMargin(true);
        layout.setSpacing(true);
        layout.setWidth("320px");
        
        MaduraPropertyWrapper maduraPropertyWrapper = maduraSessionManager.getMaduraPropertyWrapper(fm);
        FieldFactory fieldFactory = maduraSessionManager.getFieldFactory();
        final Field field = fieldFactory.createFieldByPropertyType(maduraPropertyWrapper);
        addComponent(field);
        final Window window = getWindow();
        Button buttonNotknown = fieldFactory.createButton("NotKnown", new Button.ClickListener(){

			private static final long serialVersionUID = 1L;

			public void buttonClick(ClickEvent event) {
				Window parent = window.getParent();
				parent.removeWindow(window);
				rulesPlugin.setNotKnown(fm);
				FieldMetadata fm1 =  rulesPlugin.getEmptyField(fieldMetadata);
				logger.debug("Found empty field {}",(fm1==null)?"null":fm1.getName());
				if (fm1 != null)
				{
					OneFieldWindow ofw = new OneFieldWindow(rulesPlugin,fm1, fieldMetadata, maduraSessionManager);
					parent.addWindow(ofw);
				}
				else
				{
					OneFieldWindow ofw = new OneFieldWindow(rulesPlugin,fieldMetadata, maduraSessionManager);
					parent.addWindow(ofw);
				}
			}});
        addComponent(buttonNotknown);
        Button buttonOK = fieldFactory.createButton("OK", new Button.ClickListener(){

			private static final long serialVersionUID = 1L;

			public void buttonClick(ClickEvent event) {
				Window parent = window.getParent();
				parent.removeWindow(window);
				logger.debug("set {} to {}",fm.getName(), field.getValue());
				FieldMetadata fm1 =  rulesPlugin.getEmptyField(fieldMetadata);
				logger.debug("Found empty field {}",(fm1==null)?"null":fm1.getName());
				if (fm1 != null)
				{
					OneFieldWindow ofw = new OneFieldWindow(rulesPlugin,fm1,fieldMetadata, maduraSessionManager);
					parent.addWindow(ofw);
				}
				else
				{
					OneFieldWindow ofw = new OneFieldWindow(rulesPlugin, fieldMetadata, maduraSessionManager);
					parent.addWindow(ofw);
				}
			}});
        addComponent(buttonOK);

        field.focus();
        layout.setComponentAlignment(buttonNotknown, Alignment.BOTTOM_RIGHT);
        layout.setComponentAlignment(buttonOK, Alignment.BOTTOM_RIGHT);
	}

	private OneFieldWindow(RulesPlugin rulesPlugin, FieldMetadata fieldMetadata, MaduraSessionManager maduraSessionManager) {
		setModal(true);
        VerticalLayout layout = (VerticalLayout) getContent();
        layout.setMargin(true);
        layout.setSpacing(true);
        layout.setWidth("320px");
        
        MaduraPropertyWrapper maduraPropertyWrapper = maduraSessionManager.getMaduraPropertyWrapper(fieldMetadata);
        FieldFactory fieldFactory = maduraSessionManager.getFieldFactory();
        Field field = fieldFactory.createFieldByPropertyType(maduraPropertyWrapper);
        field.setReadOnly(true);
        addComponent(field);
        final Window window = getWindow();
        Button buttonOK = fieldFactory.createButton("OK", new Button.ClickListener(){

			private static final long serialVersionUID = 1L;

			public void buttonClick(ClickEvent event) {
				Window parent = window.getParent();
				parent.removeWindow(window);
			}});
        addComponent(buttonOK);

        layout.setComponentAlignment(buttonOK, Alignment.BOTTOM_RIGHT);
	}
}
